//
//  ZSBMyRewardViewCell.m
//  HCNewGoldFinger
//
//  Created by hugh on 16/4/4.
//  Copyright © 2016年 zjhcsoftios. All rights reserved.
//

#import "ZSBMyRewardViewCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIColor+Ex.h"
#import <MJExtension/MJExtension.h>

#define iSEESCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)

@implementation ZSBMyRewardViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"EFEFF4"];
        [self setupView];
    }
    return self;
}

+ (CGFloat)heightForCell
{
    return 70;
}

+ (CGFloat)heightForCell:(NSString *)content
{
    NSArray *list = [ZSBMyRewardViewCell needShowList:content];
    
    return list.count*20+30;
}

+ (NSArray *)needShowList:(NSString *)content
{
    NSMutableArray *list = [[NSMutableArray alloc] init];
    
    NSArray *array = [content mj_JSONObject];
    if ([array isKindOfClass:[NSArray class]]) {
        for (NSDictionary *dic in array) {
            if ([dic isKindOfClass:[NSDictionary class]]) {
                if ([dic[@"t"] integerValue] == 1) {
                    [list addObject:dic];
                }
            }
        }
    }
    return list;
}

- (void)setupView
{
    CGFloat height = [ZSBMyRewardViewCell heightForCell];

    _jiangliLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 5, 130, 15)];
    _jiangliLabel.font = [UIFont systemFontOfSize:12];
    _jiangliLabel.textColor = [UIColor colorWithHexString:@"333"];
    
    [self.contentView addSubview:_jiangliLabel];
    
    _chooseButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, height)];
    [_chooseButton setImage:[UIImage imageNamed:@"btn_choose"] forState:UIControlStateSelected];
    [_chooseButton addTarget:self action:@selector(chooseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_chooseButton setImage:[UIImage imageNamed:@"btn_choose_hover"] forState:UIControlStateSelected];
    _chooseButton.hidden = YES;
    

//    _nameLabel = [ZSBUtility labelWithFrame:CGRectMake(45, 25, SCREEN_WIDTH-130, 15) font:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithHexString:@"888"]];
//    [self.contentView addSubview:_nameLabel];
//    
//    _numberLabel = [ZSBUtility labelWithFrame:CGRectMake(_nameLabel.left, _nameLabel.bottom +5, _nameLabel.width, 15) font:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithHexString:@"888"]];
//    [self.contentView addSubview:_numberLabel];
//    
//    _userTypeLabel = [ZSBUtility labelWithFrame:CGRectMake(_numberLabel.left, _numberLabel.bottom +5, _numberLabel.width, 15) font:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithHexString:@"888"]];
//    [self.contentView addSubview:_userTypeLabel];
//
//    _starRatingLabel = [ZSBUtility labelWithFrame:CGRectMake(_userTypeLabel.left, _userTypeLabel.bottom +5, _userTypeLabel.width, 15) font:[UIFont systemFontOfSize:12] textColor:[UIColor colorWithHexString:@"888"]];
//    [self.contentView addSubview:_starRatingLabel];
    
    _statusLabel = [UIButton buttonWithType:UIButtonTypeCustom];
    [_statusLabel setFrame:CGRectMake(iSEESCREEN_WIDTH-75, 20, 65, 18)];
    _statusLabel.titleLabel.font = [UIFont systemFontOfSize:12];
    _statusLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
    _statusLabel.layer.masksToBounds = YES;
    [_statusLabel.layer setCornerRadius:4];
    [_statusLabel setBackgroundColor:[UIColor colorWithHexString:@"269a69"] ];
        [_statusLabel addTarget:self action:@selector(abcda) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:_statusLabel];
    
    
    

    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, height-0.5, iSEESCREEN_WIDTH, 0.5)];
    _line.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
    [self.contentView addSubview:_line];
    //转派
    _zhuan = [[UIImageView alloc] initWithFrame:CGRectMake(185, 5, 12, 12)];


    _zhuan.image =[UIImage imageNamed:@"icon_zhuan"];
    //转派
    _bao = [[UIImageView alloc] initWithFrame:CGRectMake(200, 5, 12, 12)];
    _bao.image = [UIImage imageNamed:@"bao"];
    _ti = [[UIImageView alloc] initWithFrame:CGRectMake(215, 5, 12, 12)];
    _ti.image = [UIImage imageNamed:@"ti"];
    _tuo = [[UIImageView alloc] initWithFrame:CGRectMake(230, 5, 12, 12)];
    _tuo.image = [UIImage imageNamed:@"tuo"];

    _qiangLaber =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [self.contentView addSubview:_qiangLaber];
    
    
    
}

- (void)setContent:(NSString *)content
{
    _content = content;
    
    if ([_logid isEqualToString:@"523"]||[_logid isEqualToString:@"524"]||[_logid isEqualToString:@"525"]) {
    }else
    {
        [self.contentView addSubview:_chooseButton];
    }
    
    if ([_logid isEqualToString:@"520"]||[_logid isEqualToString:@"521"]||[_logid isEqualToString:@"522"] ||[_logid isEqualToString:@"526"] ||[_logid isEqualToString:@"548"]) {

        [_statusLabel setTitle:_myRewardInfo.status forState:UIControlStateNormal];
        
        if ([_statusLabel.titleLabel.text isEqualToString:@"订单流程"]) {
            [_statusLabel setBackgroundColor:[UIColor orangeColor]];
        }
       }
    
    
    NSArray *array = [ZSBMyRewardViewCell needShowList:content];
//    CGFloat left = 15.0;
    if (_needChoose) {
//        left = 40.0;
        _chooseButton.hidden = NO;
    }else{
        _chooseButton.hidden = YES;
    }
    _chooseButton.frame = CGRectMake(0, 0, 40, [ZSBMyRewardViewCell heightForCell:content]);
    
    
    
    
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
//        if ([dic[@"k"] isEqualToString:@"客户名"]) {
//            _nameLabel.text = [NSString stringWithFormat:@"%@:%@",dic[@"k"],dic[@"v"]];
//        }else if([dic[@"k"] isEqualToString:@"业务号码"] ){
//            _numberLabel .text = [NSString stringWithFormat:@"%@:%@",dic[@"k"],dic[@"v"]];
//        }else{
//            _jiangliLabel.text = [NSString stringWithFormat:@"%@:%@",dic[@"k"],dic[@"v"]];
//        }
        UILabel *label = [self viewWithTag:100 + i];
        if (label) {
            
            label.text = [NSString stringWithFormat:@"%@:%@",dic[@"k"],dic[@"v"]];
        }else {
            
            label = [[UILabel alloc] initWithFrame:CGRectMake(45, 25 + i * (15 + 5) , iSEESCREEN_WIDTH-130, 15)];
            label.font = [UIFont systemFontOfSize:12];
            label.textColor = [UIColor colorWithHexString:@"888"];
            label.tag = 100 + i;
            [self.contentView addSubview:label];
        }
        label.text = [NSString stringWithFormat:@"%@:%@",dic[@"k"],dic[@"v"]];

    }
    
    for (UIView *view in self.contentView.subviews) {
        if (view.tag - 100 >= array.count && [view isKindOfClass:[UILabel class]] && view != _jiangliLabel) {
            [view removeFromSuperview];
        }
    }
    
    _line.frame = CGRectMake(0, [ZSBMyRewardViewCell heightForCell:content] - .5, iSEESCREEN_WIDTH, .5);
}
- (void)setNeedChoose:(BOOL)needChoose
{
    _needChoose = needChoose;
    [self setContent:_content];
}

- (void)chooseButtonClick:(UIButton *)button
{
    button.selected = !button.selected;
    _myRewardInfo.isSelect = button.selected;
    if ([self.delegate respondsToSelector:@selector(cell:didSelectAtIndexPath:buttonIndex:)]) {
        [self.delegate cell:self didSelectAtIndexPath:self.indexPath buttonIndex:0];
    }
}
-(void)abcda{
    
    if ([self.delegate respondsToSelector:@selector(cell:didSelectAtIndexPath:buttonIndex:)]) {
        [self.delegate cell:self didSelectAtIndexPath:self.indexPath buttonIndex:0];
    }
}

-(void)setMyRewardInfo:(ZSBMyRewardInfo *)myRewardInfo{
    _myRewardInfo = myRewardInfo;
    if (_myRewardInfo.qdbz == 1) {
   
        _qiangLaber.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"qiang"]];
    
    }else{
        _qiangLaber.backgroundColor = [UIColor clearColor];
//        [_qiangLaber setBackgroundImage:nil];
    }
    if (_myRewardInfo.zpbz == 1) {
    
        _zhuan.hidden = NO;
//        [self.contentView addSubview:_zhuan];
    }else{
        _zhuan.hidden = YES;
//        [_zhuan removeFromSuperview];
    }
    
    _bao.hidden = _myRewardInfo.baoFlag;
    
    _ti.hidden = _myRewardInfo.tiFlag;
    
    _tuo.hidden = _myRewardInfo.tuoFlag;
    
//    if (_myRewardInfo.baoFlag) {
//        [self.contentView addSubview:_bao];
//    }else{
//        [_bao removeFromSuperview];
//    }
//    if (_myRewardInfo.tiFlag) {
//        [self.contentView addSubview:_ti];
//    }else{
//        [_ti removeFromSuperview];
//    }
//    if (_myRewardInfo.tuoFlag) {
//        [self.contentView addSubview:_tuo];
//    }else{
//        [_tuo removeFromSuperview];
//    }

    _jiangliLabel.text = [NSString stringWithFormat:@"%@",myRewardInfo.commission];
}





@end
